/**
 *
 *
 * @author TOTORO
 * v0.1
 * @date 2021/5/7 14:52
 */

isPswd = false, isSms = false;
$(function () {
    // 默认为密码登录
    var form = $("#login-form");
    form.append(password());
    $("#password-login").click(function () {
        if(!isPswd) {
            // 提示组件
            var error = $("#login-error");
            error.hide();
            $("#url").nextAll().remove();
            form.append(password())
            $("#sms-login").css('border-bottom', 'none');
            $(this).css('border-bottom', '2px solid #000');
            isPswd = true;
            isSms = false;
        }
    });
    $("#sms-login").click(function () {
        if(!isSms){
            // 提示组件
            var error = $("#login-error");
            error.hide();
            $("#url").nextAll().remove();
            form.append(phoneCode());
            $("#password-login").css('border-bottom','none');
            $(this).css('border-bottom', '2px solid #000');
            isSms = true;
            isPswd = false;
        }
    });

    /**
     * 点击密码登录
     */
    $("#login-form").on('click', ".password-login", function () {
        var nameVal = $(".input-wrap-loginid>input[name='user']").val();
        var passwordVal = $(".input-wrap-loginid>input[name='password']").val();
        // 提示组件
        var error = $("#login-error");
        var emsg = $(".login-error-msg");
        // 用户名是否为空
        var isNameAir = "" == nameVal || null == nameVal;
        var isPasswordAir = "" == passwordVal || null == passwordVal;
        if(isNameAir){
            $(".input-wrap-loginid>input[name='user']").focus();
            error.show();
            emsg.text("请输入账户名");
            return false;
        } else if(isPasswordAir){
            error.show();
            emsg.text("请输入密码");
            return false;
        }

        // 判断是否是从其他页面过来的，如果是的话，返回之前的

        var  forward =document.referrer;
        if(forward==""||forward==undefined||forward==null){
            forward="/";
        }
        else{
            location.href=""+forward+"";
        }
    });

    /**
     * 点击验证码登录
     */
    $("#login-form").on('click', ".sms-login", function () {
        var phoneVal = $("#login-phone").val();
        var codeVal = $("#login-code").val();
        // 判断 手机格式是否正确
        var regex = /^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/;
        var qualified = regex.test(phoneVal)
        // 提示组件
        var error = $("#login-error");
        var emsg = $(".login-error-msg");
        // 用户名是否为空
        var isPhoneAir = "" == phoneVal || null == phoneVal;
        var isCodeAir = "" == codeVal || null == codeVal;
        if(isPhoneAir){
            $("#login-phone").focus();
            error.show();
            emsg.text("请输入手机号码");
            return false;
        } else if (!qualified){
            error.show();
            emsg.text("手机号码格式不正确，请重新输入");
            return false;
        } else if(isCodeAir){
            error.show();
            emsg.text("请输入短信验证码");
            return false;
        }

        var  forward =document.referrer;
        if(forward==""||forward==undefined||forward==null){
            forward="/";
        }
        else{
            location.href=""+forward+"";
        }
    });

    // 点击发送验证码
    $("#login-form").on('click','.send-btn-link',function () {
        // 获取手机号
        var phone = $("#login-phone").val();
        // 手机格式验证
        var regex = /^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/;
        if("" == phone || null == phone){
            $("#login-error").show();
            $(".login-error-msg").text("请输入手机号码");
        } else if(!regex.test(phone) && phone.length>0){
            $("#login-error").show();
            $(".login-error-msg").text("手机号码格式不正确，请重新输入");
        } else {
            $.ajax({
                type: "POST",
                url: "${pageContext.request.contextPath}/login/send_code.htm",
                async: false,
                data: {"phone":phone},
                success: function (dt) {
                    $(".sms-send-success-tip").show();
                    var btn = $(".send-btn-link");
                    var time = 59;
                    btn.text((time + 1) + "秒后重发)").prop("disabled", true);
                    var timer = setInterval(function () {
                        if (time == 0) {
                            btn.text("重新发送");
                            btn.prop("disabled", false);
                            clearInterval(timer);
                        } else {
                            btn.text(time + "秒后重发");
                            time--;
                        }
                    }, 1000);
                }
            });
        }
    });

});

/**
 * 密码登录
 * @returns {*|Window.jQuery|HTMLElement}
 */
function password() {
    return $("<div class=\"fm-field\">\n" +
        "                                               <div>\n" +
        "                                                   <input type='hidden' value='user' name='type'>"+
        "                                                   <label class=\"fm-label-icon\">\n" +
        "                                                       <i>\n" +
        "                                                           <svg t=\"1620373325823\" class=\"icon\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"3975\" width=\"24\" height=\"26\"><path d=\"M619.5134375 487.878125c69.99375-37.0734375 108.43125-106.2225 108.43125-186.15C728.0103125 185.050625 631.071875 90.125 511.9259375 90.125c-119.20875 0-216.1471875 94.925625-216.1471875 211.603125 0 79.99125 35.0615625 149.0765625 105.054375 186.15C241.8228125 532.420625 123.2628125 686.8221875 123.2628125 859.2078125c0 35.775 2.53125 73.8890625 80.5115625 74.4740625 69.60375 0.5184375 472.7465625-0.1959375 616.56375 0 63.825 0.06375 80.056875-37.14 80.3821875-74.4740625 1.6875-170.8275-121.9359375-326.7871875-281.206875-371.3278125z\" p-id=\"3976\" fill=\"#ffffff\"></path></svg>\n" +
        "                                                       </i>\n" +
        "                                                   </label>\n" +
        "                                               </div>\n" +
        "                                               <div class=\"input-plain-wrap input-wrap-loginid\">\n" +
        "                                                   <input class=\"fm-text\" type=\"text\" placeholder=\"会员名/邮箱/手机号\" aria-label=\"会员名/邮箱/手机号\" autocapitalize=\"off\" name='user'>\n" +
        "                                               </div>\n" +
        "                                           </div>\n" +
        "\n" +
        "                                           <div class=\"fm-field\">\n" +
        "                                               <div>\n" +
        "                                                   <label class=\"fm-label-icon\">\n" +
        "                                                       <i>\n" +
        "                                                           <svg t=\"1620373711610\" class=\"icon\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"4887\" width=\"24\" height=\"26\"><path d=\"M809.6 416.64h-53.76V308.48c0-135.68-108.096-243.84-243.2-243.84-135.04 0-243.2 108.16-243.2 243.84v108.16h-53.76c-29.44 0-53.76 24.32-53.76 53.76v433.28c0 29.44 24.32 53.76 53.76 53.76h593.92c30.08 0 54.4-24.32 54.4-53.76V470.4c0-29.44-24.32-53.76-54.4-53.76z m-135.04 0H350.72V308.48c0-89.6 72.96-162.56 161.92-162.56a162.56 162.56 0 0 1 161.92 162.56v108.16z\" p-id=\"4888\" fill=\"#ffffff\"></path></svg>\n" +
        "                                                       </i>\n" +
        "                                                   </label>\n" +
        "                                               </div>\n" +
        "                                               <div class=\"input-plain-wrap input-wrap-loginid\">\n" +
        "                                                   <input class=\"fm-text\" type=\"password\" placeholder=\"请输入登录密码\" aria-label=\"请输入登录密码\" autocapitalize=\"off\" name='password'>\n" +
        "                                               </div>\n" +
        "                                           </div>\n" +
        "\n" +
        "                                           <div id='pns' class=\"fm-field\" style='display: none'>\n" +
        "                                               <div id=\"drag\"></div>\n" +
        "                                               <script type=\"text/javascript\">\n" +
        "                                                   $('#drag').sliding();\n" +
        "                                               </script>\n" +
        "                                           </div>\n" +
        "\n" +
        "                                           <div class=\"fm-btn\">\n" +
        "                                               <button type=\"submit\" tabindex=\"3\" class=\"fm-button fm-submit password-login\">登录</button>\n" +
        "                                           </div>\n" +
        "\n" +
        "                                           <div class=\"login-links\">\n" +
        "                                               <a target='_blank' href='/user/find_password.htm'>忘记密码</a>\n" +
        "                                               <a target=\"_blank\" href='/register_01_mobile.htm'>免费注册</a>\n" +
        "                                           </div>");
}

/**
 * 短信登录
 * @returns {*|Window.jQuery|HTMLElement}
 */
function phoneCode() {
    return $(" <div class=\"fm-field\">\n" +
        "                                               <div>\n" +
        "                                                   <input type='hidden' value='phone' name='type'>"+
        "                                                   <label class=\"fm-label-icon\">\n" +
        "                                                       <i>\n" +
        "                                                           <svg t=\"1620379103086\" class=\"icon\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"13762\" width=\"30\" height=\"26\"><path d=\"M197.4 119.5v786c0 33.1 26.9 60 60 60h514c33.1 0 60-26.9 60-60v-786c0-33.1-26.9-60-60-60h-514c-33.1 0-60 26.9-60 60z m240-15h154c5.5 0 10 4.5 10 10v4c0 5.5-4.5 10-10 10h-154c-5.5 0-10-4.5-10-10v-4c0-5.5 4.5-10 10-10z m79.4 831.9c-26.7 1.4-48.6-20.6-47.3-47.3 1.2-22.9 19.7-41.4 42.6-42.6 26.7-1.4 48.6 20.6 47.3 47.3-1.2 22.9-19.8 41.5-42.6 42.6z m201.1-121.9h-407c-11 0-20-9-20-20v-604c0-11 9-20 20-20h407c11 0 20 9 20 20v604c0 11-8.9 20-20 20z\" p-id=\"13763\" fill=\"#ffffff\"></path></svg>\n" +
        "                                                       </i>\n" +
        "                                                   </label>\n" +
        "                                               </div>\n" +
        "                                               <div class=\"native-phone-code-select-wrap\" data-content=\"+86\">\n" +
        "                                                   <select name=\"nativePhoneCodeSelector\" class=\"native-phone-code-select\">\n" +
        "                                                       <option style=\"margin-left: 100px;\">+86 中国大陆</option>\n" +
        "                                                       <option>+852 中国香港</option>\n" +
        "                                                       <option>+853 中国澳门</option>\n" +
        "                                                       <option>+886 中国台湾</option>\n" +
        "                                                   </select>\n" +
        "                                               </div>\n" +
        "                                               <div class=\"input-plain-wrap input-wrap-sms-loginid\">\n" +
        "                                                   <input class=\"fm-text\" type=\"text\" placeholder=\"请输入手机号\" aria-label=\"请输入手机号\" autocapitalize=\"off\" name='phone' id='login-phone'>\n" +
        "                                               </div>\n" +
        "                                           </div>\n" +
        "                                           <div class=\"fm-field fm-field-sms\">\n" +
        "                                               <div>\n" +
        "                                                   <label class=\"fm-label-icon\">\n" +
        "                                                       <i>\n" +
        "                                                           <svg t=\"1620378936620\" class=\"icon\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"11116\" width=\"24\" height=\"26\"><path d=\"M242.816 171.434667h535.466667c39.381333 0 71.338667 32 71.338666 71.381333v535.466667c0 39.381333-31.957333 71.338667-71.381333 71.338666H242.816c-39.424 0-71.381333-31.957333-71.381333-71.381333V242.816c0-39.424 32-71.381333 71.381333-71.381333z m0 49.962666a21.418667 21.418667 0 0 0-21.418667 21.418667v535.466667c0 11.776 9.6 21.376 21.418667 21.376h535.466667a21.418667 21.418667 0 0 0 21.376-21.418667V242.816a21.418667 21.418667 0 0 0-21.418667-21.418667H242.816z\" fill=\"#ffffff\" p-id=\"11117\"></path><path d=\"M271.36 332.074667v392.618666h50.005333V332.074667H271.36z m232.021333 42.837333v128.469333H378.453333v228.48h174.933334v-78.506666h-50.005334v28.501333h-74.922666V553.386667h124.928V324.906667h-174.933334v78.506666h50.005334v-28.501333h74.922666z m195.754667 306.944h-99.370667v50.005333h149.333334V324.906667h-149.333334v50.005333h99.413334v306.944z\" fill=\"#ffffff\" p-id=\"11118\"></path><path d=\"M599.765333 535.509333h124.373334v-49.92h-124.373334z\" fill=\"#ffffff\" p-id=\"11119\"></path></svg>\n" +
        "                                                       </i>\n" +
        "                                                   </label>\n" +
        "                                               </div>\n" +
        "                                               <div class=\"input-plain-wrap input-wrap-smscode\">\n" +
        "                                                   <input class=\"fm-text\" type=\"text\" placeholder=\"请输入验证码\" aria-label=\"请输入验证码\" autocapitalize=\"off\" name='code' id='login-code'>\n" +
        "                                               </div>\n" +
        "                                               <div class=\"send-btn\">\n" +
        "                                                   <a class=\"send-btn-link\">获取验证码</a>\n" +
        "                                               </div>\n" +
        "                                           </div>\n" +
        "                                           <div id='cns' class=\"fm-field\" style='display: none'>\n" +
        "                                               <div id=\"drag\"></div>\n" +
        "                                               <script type=\"text/javascript\">\n" +
        "                                                   $('#drag').sliding();\n" +
        "                                               </script>\n" +
        "                                           </div>\n" +
        "                                           <div class='sms-send-success-tip' style='margin-bottom: 10px; display: none'>" +
        "                                               <i>" +
        "                                                   <svg style='position: relative; top: 4px' t=\"1620442120898\" class=\"icon\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"1644\" width=\"18\" height=\"18\"><path d=\"M512 65.6c247.424 0 448 200.576 448 448S759.424 961.6 512 961.6 64 761.024 64 513.6 264.576 65.6 512 65.6z m0 48C291.0864 113.6 112 292.6864 112 513.6s179.0864 400 400 400 400-179.0864 400-400S732.9136 113.6 512 113.6z m149.568 279.12c8.488-10.1808 23.6224-11.552 33.8016-3.0624 10.1792 8.4896 11.5504 23.624 3.0608 33.8032L522.96 633.872c-22.72 27.0768-63.088 30.608-90.1648 7.888l-104.1984-87.4336c-10.1552-8.52-11.4784-23.6576-2.96-33.8112 8.5216-10.1536 23.6592-11.4784 33.8128-2.96l104.1984 87.4352c6.7696 5.68 16.8624 4.7968 22.496-1.9168z\" fill=\"#B8F866\" p-id=\"1645\"></path></svg>"+
        "                                               </i>" +
        "                                                短信验证码已发送，可能会有延后请耐心等待"+
        "                                           </div>"+
        "                                           <div class=\"fm-btn\">\n" +
        "                                               <button type=\"submit\" tabindex=\"3\" class=\"fm-button fm-submit sms-login\">登录</button>\n" +
        "                                           </div>\n" +
        "                                           <div class=\"login-links\">\n" +
        "                                               <a target=\"_blank\" href='register_01_mobile.html'>免费注册</a>\n" +
        "                                           </div>");
}
